package cn.itcast.wushangxiantu.Activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputFilter;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ExpandableListView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.itcast.wushangxiantu.BaseAdapter.ExpandAdapter;
import cn.itcast.wushangxiantu.UserData.HeChengYulan;
import cn.itcast.wushangxiantu.UserData.User;
import cn.itcast.wushangxiantu.OkhttpUtil;
import cn.itcast.wushangxiantu.R;
import cn.itcast.wushangxiantu.ToastUtil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class HeCheng extends AppCompatActivity {

    private ExpandableListView expandContent;
    private HeChengYulan heChengYulan;
    private String name[];
    private User user;
    int num = 0;
    private ExpandableListView expandContent2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_he_cheng);
        initView();
    }

    private void initView() {
        expandContent = (ExpandableListView) findViewById(R.id.expandContent);
        getWuPin();
    }

    private void getWuPin() {
        Resources res = getResources();
        String[] wuqi = res.getStringArray(R.array.wuqi);
        String[] hujia = res.getStringArray(R.array.hujia);
        String[] jiezhi = res.getStringArray(R.array.jiezhi);
        String[] zuju = res.getStringArray(R.array.zuju);
        String[] lingshi2 = res.getStringArray(R.array.lingshi2);
        String[] lingshi1 = res.getStringArray(R.array.lingshi1);
        name = res.getStringArray(R.array.daoju);
        List<String[]> list = new ArrayList<>();
        list.add(wuqi);
        list.add(hujia);
        list.add(jiezhi);
        list.add(zuju);
        list.add(lingshi1);
        list.add(lingshi2);
        ExpandAdapter expandableListView = new ExpandAdapter(HeCheng.this, list);
        expandContent.setAdapter(expandableListView);
        expandContent.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
                getwupin(i * 10 + i1 + 1);
                num = i * 10 + i1 + 1;
                return false;
            }
        });
    }

    private void getHeCheng(int id) {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "need=hecheng" + "&id=" + id + "&userName=" + Login.getUser().getUsernumber(), new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("Hecheng1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("Hecheng2", src);
                try {
                    user = new Gson().fromJson(src, User.class);
                } catch (Exception e) {
                    user = null;
                }
                Message message = new Message();
                message.obj = user;
                message.what = 2;
                handler.sendMessage(message);
            }
        });
    }
    private void getHeCheng(int id,int wpnumber) {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "need=hechengs" + "&id=" + id + "&userName=" + Login.getUser().getUsernumber()+"&number="+wpnumber, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("Hecheng1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("Hecheng2", src);
                try {
                    user = new Gson().fromJson(src, User.class);
                } catch (Exception e) {
                    user = null;
                }
                Message message = new Message();
                message.obj = user;
                message.what = 2;
                handler.sendMessage(message);
            }
        });
    }
    private void getwupin(int id) {
        Log.v("213131213", "" + id);
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "need=hechengyulan" + "&id=" + id, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("Login111", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("Login222", src);
                try {
                    heChengYulan = new Gson().fromJson(src, HeChengYulan.class);
                } catch (Exception e) {
                    heChengYulan = null;
                }
                Message message = new Message();
                message.obj = heChengYulan;
                message.what = 1;
                handler.sendMessage(message);
            }
        });

    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message message) {
            switch (message.what) {
                case 1:
                    heChengYulan = (HeChengYulan) message.obj;
                    if (heChengYulan != null) {
                        if (heChengYulan.getStw().equals("OK")) {
                            GeneralDialog("您确定打造此物品吗", jisuan(heChengYulan));
                            //弹出对话框
                        } else {
                            ToastUtil.toast(HeCheng.this, heChengYulan.getStw());
                        }
                    } else {
                        ToastUtil.toast(HeCheng.this, "服务器炸了");
                    }
                    break;
                case 2:
                    user = (User) message.obj;
                    if (null != user) {
                        if (user.getStw().equals("OK")) {
                            ToastUtil.toast(HeCheng.this, "打造成功");
                            Login.getUser().setMoney(user.getMoney());
                        } else {
                            ToastUtil.toast(HeCheng.this, user.getStw());
                        }
                    } else {
                        ToastUtil.toast(HeCheng.this, "服务器坏掉了。。。");
                    }
                    break;
            }
            return false;
        }
    });

    private String jisuan(HeChengYulan heChengYulan) {
        String content = "";
        content = "物品名称；" + heChengYulan.getName() + "\n" +
                "使用等级：" + heChengYulan.getSydj() + "\n******装备属性******\n";
        switch (heChengYulan.getRegion()) {
            case 2:
                content += "攻击力：" + heChengYulan.getJsdj() * 40 + "--" + (heChengYulan.getJsdj() * 40 + heChengYulan.getJsdj() * 6) + "\n" +
                        "暴击率：" + (int) heChengYulan.getJsdj() / 5 + "--" + (int) (heChengYulan.getJsdj() / 5 + (int) heChengYulan.getJsdj() * 0.3) + "\n";
                break;
            case 3:
                content += "生命：" + heChengYulan.getJsdj() * 100 + "--" + (heChengYulan.getJsdj() * 100 + heChengYulan.getJsdj() * 6) + "\n" +
                        "防御力：" + heChengYulan.getJsdj() * 20 + "--" + (heChengYulan.getJsdj() * 20 + heChengYulan.getJsdj() * 3) + "\n";
                break;
            case 4:
                content += "命中：" + (int) heChengYulan.getJsdj() / 5 + "--" + (int) (heChengYulan.getJsdj() / 5 + (int) heChengYulan.getJsdj() * 0.3) + "\n" +
                        "穿透：" + heChengYulan.getJsdj() * 10 + "--" + (heChengYulan.getJsdj() * 10 + heChengYulan.getJsdj() * 6) + "\n";
                break;
            case 5:
                content += "闪避：" + (int) heChengYulan.getJsdj() / 5 + "--" + (int) (heChengYulan.getJsdj() / 5 + (int) heChengYulan.getJsdj() * 0.3) + "\n" +
                        "恢复：" + heChengYulan.getJsdj() * 10 + "--" + (heChengYulan.getJsdj() * 10 + heChengYulan.getJsdj() * 6) + "\n";
                break;
        }
        if (heChengYulan.getDz1number() == heChengYulan.getDz2number() && heChengYulan.getDz2number() == heChengYulan.getDz3number()) {
            content += "******所需道具******\n" + name[heChengYulan.getDz1() - 1] + "*" +
                    heChengYulan.getDz1number() + "\n消耗金币：0";
        } else {
            content += "******所需道具******\n" + name[heChengYulan.getDz1() - 1] + "*" + heChengYulan.getDz1number() + "\n" + name[heChengYulan.getDz2() - 1] + "*" + heChengYulan.getDz2number() + "\n"
                    + name[heChengYulan.getDz3() - 1] + "*" + heChengYulan.getDz3number() + "\n消耗金币：" + heChengYulan.getJsdj() * heChengYulan.getJsdj() * 10;
        }
        return content;
    }

    public void GeneralDialog(String head, final String content) {
        /**
         * 普通对话框
         * 1标题
         * 2内容
         */
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle(head)

                .setMessage(content)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        getHeCheng(num);
                        dialogInterface.dismiss();
                    }
                })
                .setNeutralButton("批量", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        GeneralDialogDingJia(HeCheng.this);
                    }
                });
        dialog = builder.create();
        dialog.show();
    }
    private void GeneralDialogDingJia(final Context context) {
        /**
         * 编辑框
         * 1标题
         */
        AlertDialog dialog;
        final EditText editText = new EditText(context);
        editText.setInputType(InputType.TYPE_CLASS_NUMBER);
        InputFilter[] filters = {new InputFilter.LengthFilter(9)};
        editText.setFilters(filters);
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setTitle("输入批量打造个数")
                .setView(editText)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (editText.getText().toString().trim().equals("") || editText.getText().length() >= 16) {
                            dialogInterface.dismiss();
                            ToastUtil.toast(context, "输入有误");
                            return;
                        }
                        int chushouNum = Integer.parseInt(editText.getText().toString().trim());
                        if (chushouNum < 0) {
                            ToastUtil.toast(context, "输入有误");
                        } else {
                            getHeCheng(num,chushouNum);
                        }
                    }
                });
        dialog = builder.create();
        dialog.show();
    }
}
